﻿using System;

namespace Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
            Test.GetInstance().DO();
        }
    }

    #region 泛型继承单例
    public class Singleton<T> where T : new()
    {
        private static T _instance;
        private static readonly object syslock = new object();
        public static T GetInstance()
        {
            if (_instance == null)
            {
                lock (syslock)
                {
                    if (_instance == null)
                    {
                        _instance = new T();
                        return _instance;
                    }
                    else
                    {
                        return _instance;
                    }
                }
            }
            else
            {
                return _instance;
            }
        }

    }

    public class Test : Singleton<Test>
    {
        public void DO()
        {
            Console.WriteLine("执行");
        }
    } 
    #endregion

}
